<template>
	<div style="height:3000px">
		<!-- 项目信息维护 --页面 -->
		<v-card>
			<v-card-title>
				<div>
					<v-btn class="ml-2 mt-1" small tile color="primary" @click="editProjectClick">
						新增
					</v-btn>
					<v-btn class="ml-2 mt-1" small tile color="primary" @click="openEmailDialog">
						邮件发送
					</v-btn>

				</div>
				<v-divider class="mx-4" inset vertical></v-divider>
				<v-row class="mt-2">
					<v-col cols="12" sm="3" md="2">
						<v-text-field label="项目号" v-model="projectQuery.code" dense outlined>
						</v-text-field>
					</v-col>
					<v-col cols="12" sm="3" md="2">
						<v-text-field label="项目名称" v-model="projectQuery.name" dense outlined>
						</v-text-field>
					</v-col>
					<!-- sm="6" md="2" -->
					<v-col cols="12" sm="3" md="2">
						<v-select v-model="projectQuery.type" :items="queryTypeItems" label="项目类型" dense outlined>
						</v-select>
					</v-col>
					<v-col cols="12" sm="3" md="2">
						<v-text-field label="设备名称" v-model="projectQuery.equiname" dense outlined>
						</v-text-field>
					</v-col>
					<v-col cols="12" sm="3" md="2">
						<v-select label="项目审核状态" v-model="projectQuery.status" :items="ApprovalStatusItems" dense
							outlined>
						</v-select>
					</v-col>

					<!-- ml-n16 前面加n是间距 -->
					<!-- class="ml-n16 mx-auto" -->
					<v-col cols="12" sm="3" md="2">
						<v-btn depressed color="primary" @click="loadPages">查询</v-btn>
						<v-btn class="ml-3" depressed color="primary" @click="refreshLoadPages">
							刷新</v-btn>
					</v-col>
				</v-row>
			</v-card-title>
			<v-card-text>
				<v-data-table :headers="projectHeaderList" :items="projectDataList" small fixed-header item-key="id"
					dense show-select class="text-no-wrap" loading-text="正在加载请稍等......" :loading="projectLoading"
					:options.sync="projectOptions" :footer-props="vuetifyDefaultPage"
					:server-items-length="projectTotalCount" @input="selectProjectMultiple">
					<template v-slot:item.actions="{ item }">
						<div>
							<v-btn class="ml-2" small tile color="primary" @click="editProjectClick(item)">
								编辑项目
							</v-btn>
							<v-btn class="ml-2" small tile color="primary" @click="openEquiEdit(item)">
								维护设备清单
							</v-btn>
						<!-- 	<v-btn class="ml-2" small tile color="green" :disabled="(item.isissue&&item.approvalstatus==99)||(item.isissue==false&&item.approvalstatus!=99)"
								@click="projectsIssueClick(item)">
								项目下发
							</v-btn> -->
									<v-btn class="ml-2" small tile color="red" @click="deleteProjectClick(item)">
						删除
					</v-btn>
						</div>
					</template>
					<template v-slot:item.approvalstatusname="{ item }">
						<a v-if="item.approvalstatus==99" style="color: #67C23A;">{{item.approvalstatusname}}</a>
						<a v-else-if="item.approvalstatus==-1" style="color: red;">{{item.approvalstatusname}}</a>
						<a v-else>{{item.approvalstatusname}}</a>
					</template>
					<template v-slot:item.typename="{ item }">
						<a v-if="item.typename=='正式项目'" style="color: #67C23A;">正式项目</a>
						<a v-else-if="item.typename=='机会项目'">机会项目</a>
					</template>

					<template v-slot:item.isissue="{ item }">
						<a v-if="item.isissue" style="color: #67C23A;">已下发</a>
						<a v-else style="color: #C7C7C7;">否</a>
					</template>
					<template v-slot:item.issendemail="{ item }">
						<a v-if="item.issendemail" style="color: #67C23A;">是</a>
						<a v-else style="color: #C7C7C7;">否</a>
					</template>
			<!-- 		<template v-slot:item.ishistory="{ item }">
						<a v-if="item.ishistory" style="color: #67C23A;">是</a>
						<a v-else style="color: #C7C7C7;">否</a>
					</template> -->
					<template v-slot:item.drawings="{ item }">
						<a v-if="item.planningDrawingList.length>0" style="color: #67C23A;"
							@click="drawingDialog=true,planningDrawingList=item.planningDrawingList">查看图纸</a>
					</template>
					<template v-slot:item.picurl="{ item }">
						<a @click="openLookPicsClick(item.picurl)">查看文件</a>
					</template>
				</v-data-table>
			</v-card-text>
		</v-card>
		<v-dialog v-model="drawingDialog" width="600">
			<v-card>
				<v-card-title>查看图纸</v-card-title>
				<v-card-text>
					<v-row>
						<v-col v-for="item in planningDrawingList" :key="item.id" cols="12">
							<a :href="item.link" target="_Blank">{{item.originalname}}</a>
						</v-col>
					</v-row>

				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="success" class="mr-4" text @click="drawingDialog=false">关闭</v-btn>
				</v-card-actions>
			</v-card>
		</v-dialog>
		<!-- 添加项目 -->
		<v-dialog v-model="editProjectDialog" width="600">
			<v-card>
				<v-card-title>维护项目</v-card-title>
				<v-card-text>
					<v-text-field v-model="projectModel.citemcode" label="项目号" outlined></v-text-field>
					<v-text-field v-model="projectModel.citemname" label="项目名称" outlined></v-text-field>
					<v-text-field v-model="projectModel.totalmoney" :rules="digitalRule"  label="合同总金额(单位:万)" outlined></v-text-field>
					<v-row>
						<v-col cols="12" md="5">
							<v-text-field v-model="projectModel.customercode" :style="tempCustomer" disabled label="客户编码" outlined dense>
							</v-text-field>
						</v-col>
						<v-col cols="12" md="5">
							<v-text-field v-model="projectModel.customername"  disabled label="客户名称" outlined dense>
							</v-text-field>
						</v-col>
						<v-col cols="12" md="2">
							<v-btn tile color="primary" @click="checkCustomerClick">选择客户</v-btn>
						</v-col>
					</v-row>
					
					<v-select v-model="projectModel.type" :items="queryTypeItems" label="项目类型" outlined></v-select>
					<v-select v-model="projectModel.citem_class" :disabled="projectModel.id>0"
						:items="project_fitemSelectList" label="项目大类" @input="project_fitemSelectEvent" outlined>
					</v-select>
					<v-select v-model="projectModel.citemccode" :disabled="projectModel.id>0"
						:items="project_fitemSelectChildrenList" label="项目子类" outlined>
					</v-select>
					<uploadfile :UpLoad="UpLoad" :callBackPath="callBackPath"></uploadfile>
				</v-card-text>
				<v-card-actions>
					<v-spacer></v-spacer>
					<v-btn color="success" class="mr-4" text @click="editProjectSubmit">确认</v-btn>
					<v-btn color="warning" class="mr-4" text @click="close">取消</v-btn>
				</v-card-actions>
			</v-card>

		</v-dialog>
		<!-- 添加设备清单 -->
		<equimaintenance :equiModel="equiModel" :callBack="loadPages"></equimaintenance>
		<sendemial :emailModel="emailModel" :callBack="loadPages"></sendemial>
			<customerDataList :customerCommentModel="customerCommentModel" :callBack="checkCustomerCallBack"></customerDataList>
			<showfiledialogcomment :filesModel="fileDialogComment"></showfiledialogcomment>
	</div>
</template>

<script>
	import customerDataList from '../components/CustomerDataList.vue'
	import uploadfile from '../../mes/components/UploadFile.vue'
	import equimaintenance from '../components/project_relation/info_manage/info_manage_equi_maintenance.vue'
	import sendemial from '../../mes/components/SendEmail.vue'
	import showfiledialogcomment from '../../comments/ShowFilesDialog.vue'
	export default {
		name: 'project_relation_info_maintain',
		components: {
			uploadfile,
			equimaintenance,
			sendemial,
			customerDataList,
			showfiledialogcomment
		},
		data: () => ({
			fileDialogComment: {
				dialog: false,
				files: []
			},
			digitalRule: [
				v => /^[0-9]*$/.test(v) || '请输入合法数字'
			],
			queryTypeItems: [{
				text: '机会项目',
				value: 1
			}, {
				text: '正式项目',
				value: 2
			}],
			ApprovalStatusItems: [],
			projectQuery: {
				code: '',
				name: '',
				equiname: '',
				status: null,
				type: 0
			},
			projectHeaderList: [],
			projectDataList: [],
			projectTotalCount: 0,
			projectLoading: true,
			projectOptions: { //页数
				page: 1,
				//每页行数
				itemsPerPage: 10
			},
			vuetifyDefaultPage: {
				'items-per-page-text': '每页个数',
				'items-per-page-options': [10, 20, 50, 100]
				// 'items-per-page-all-text': '100'
			},
			projectModel: {},
			project_fitemSelectList: [],
			project_fitemSelectChildrenList: [],
			editProjectDialog: false,
			editEquipmentDetailDialog: false,
			UpLoad: {
				isUploadSingleImage: false,
				isUploadMultipleFiles: true,
				type:20 // 项目图纸
			},
			equiModel: {
				dialog: false,
				projectId: 0
			},
			drawingDialog: false,
			planningDrawingList: [],
			emailModel: {
				sendEmailDialogVisable: false,
				queryIds: [],
				isSendSelfUploadFiles: false,
				GetDepartmentUserToSelectUrl: `PDM/PDMInfoManage/GetDepartmentUserToSelect`,
				SendEmailUrl: `PDM/PDMInfoManage/SendEmail`
			},
			checkProjectList: [],
			customerCommentModel: {
				dialog: false,
				loadCustomerUrl: ``
			},
			tempCustomer:{}
		}),
		watch: {
			projectOptions: {
				handler() {
					this.loadPages()
				},
				deep: true
			},
		},
		created() {
			this.getProjectHeaderList();
			this.GetProjectFitems()
			this.loadApprovalStatusItems()
		},
		methods: {
			//查看图片
			openLookPicsClick(pathstrs) {
				this.fileDialogComment.dialog = true
				this.fileDialogComment.files =JSON.parse(pathstrs)
			},
			async loadApprovalStatusItems() {
				var response = await this.$httpRequest({
					url: `MES/MESCommon/GetTypes`,
					method: 'post',
					data: ['ApprovalStatus']
				})
				if (response.status == 1) {
					this.ApprovalStatusItems = response.data
				}
			},
			async loadPages() {
				this.projectLoading = true
				var response = await this.$httpRequest({
					url: `PDM/PDMInfoManage/LoadPage?
						code=${this.projectQuery.code}&
						name=${this.projectQuery.name}&
						ishistory=${this.projectQuery.ishistory}&
						type=${this.projectQuery.type}&
						status=${this.projectQuery.status}&
						equiname=${this.projectQuery.equiname}&
						pageNumber=${this.projectOptions.page}&
						pageSize=${this.projectOptions.itemsPerPage}`,
					method: 'get',
				})
				if (response.status == 1) {
					this.projectDataList = response.data.result
					this.projectTotalCount = response.data.pagination.totalRecords
					this.projectLoading = false
				} else {
					this.$message.error(response.info)
				}
			},
			async refreshLoadPages() {
				this.projectQuery.code = ''
				this.projectQuery.name = ''
				this.projectQuery.equiname = ''
				this.projectQuery.status = null
				this.projectQuery.type = 0
				await this.loadPages()
			},

			close() {
				this.editProjectDialog = false
				this.editEquipmentDetailDialog = false
				this.projectModel = {}
				// this.project_fitemSelectList = []
				// this.project_fitemSelectChildrenList = []
			},
			//提交项目信息
			async editProjectSubmit() {
				var url = ''
				if (this.projectModel.id > 0) {
					url = 'PDM/PDMInfoManage/UpdateProject'
				} else {
					url = 'PDM/PDMInfoManage/AddProject'
				}

				var response = await this.$httpRequest({
					url: url,
					method: 'post',
					data: this.projectModel
				})
				if (response.status == 1) {
					await this.loadPages()
					this.$message.success(response.info)
					this.close()
				} else {
					this.$message.error(response.info)
				}
			},
			//打开设备编辑对话框
			openEquiEdit(item) {
				this.equiModel.dialog = true
				this.equiModel.projectId = item.id
			},
			//编辑项目对话框
			editProjectClick(item) {

				if (item != null && item.id > 0) {
					this.projectModel = item
					this.project_fitemSelectEvent(item.citem_class)
				} else {
					this.projectModel.id = 0
				}
				this.editProjectDialog = true
			},
			async GetProjectFitems() {
				var response = await this.$httpRequest({
					url: `PDM/PDMInfoManage/GetProjectFitems`,
					method: 'get',
				})
				if (response.status == 1) {
					this.project_fitemSelectList = response.data.filter(r=>r.text=='项目管理')
				} else {
					this.$message.error(response.info)
				}
			},
			project_fitemSelectEvent(item) {
				if (item != null && item != undefined) {
					var children = this.project_fitemSelectList.filter(r => r.value == item).map(r => {
						return r.children
					})
					if (children != null && children.length > 0) {

						this.project_fitemSelectChildrenList = children[0]
					}
				} else {
					this.project_fitemSelectChildrenList = []
				}

			},
			//图片上传的回调方法
			callBackPath(data) {
				this.projectModel.picurl =JSON.stringify(data) 
			},
			//选中多个开料信息事件
			selectProjectMultiple(mulitple) {
				this.checkProjectList = mulitple
			},
			//发送邮件
			openEmailDialog() {
				this.emailModel.sendEmailDialogVisable = true
				this.emailModel.queryIds = this.checkProjectList.map(r => r.id)
	
			},
			//项目下发
			async projectsIssueClick(project) {
				var projectIdList = []
				projectIdList.push(project.id)
				var response = await this.$httpRequest({
					url: `PDM/PDMInfoManage/ProjectsIssue`,
					method: 'post',
					data: projectIdList
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			checkCustomerClick() {
				this.customerCommentModel.dialog = true
				this.customerCommentModel.loadCustomerUrl = `PDM/PDMInfoManage/LoadCustomerPage`
			},
			//选中客户
			checkCustomerCallBack(customer) {
				this.tempCustomer=customer
				this.projectModel.customercode = customer.cCusCode
				this.projectModel.customername = customer.cCusName
			
			},
		async	deleteProjectClick(item){
				var response = await this.$httpRequest({
					url: `PDM/PDMInfoManage/DeleteProject?projectid=${item.id}`,
					method: 'get'
				})
				if (response.status == 1) {
					this.$message.success(response.info)
					await this.loadPages()
				} else {
					this.$message.error(response.info)
				}
			},
			getProjectHeaderList() {
				this.projectHeaderList = [{
						text: '操作',
						align: 'center',
						value: 'actions',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '项目号',
						align: 'left',
						value: 'citemcode',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '项目名称',
						align: 'left',
						value: 'citemname',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '项目类型名称',
						align: 'center',
						value: 'typename',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '查看文件',
						align: 'center',
						value: 'picurl',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否下发到MES',
						align: 'center',
						value: 'isissue',
						filterable: false,
						class: 'fixed'
					},
					{
						text: '是否发送邮件',
						align: 'center',
						value: 'issendemail',
						filterable: false,
						class: 'fixed'
					},

					{
						text: '状态',
						align: 'center',
						value: 'approvalstatusname',
						filterable: false,
						class: 'fixed'
					}

				]
			},
		}
	}
</script>
